var  pageNumber =1;
var pageSize=5;
var pageTotal = 0;
var articleType = "";
var count =0
var title = "";
$(function(){

   $(".input_search").click(function () {
       title = $(".input_title").val()
       $(".muen_list").html("");
       list()
   })
    $(".screen_ul>li").click(function(){

        $(this)
            .addClass('selected')
            .siblings().removeClass('selected')
            .end()
            .find(':radio').attr('cheked',true)

        var id = $(this).attr("id");
        if (id==count) {
            return
        }
        $(".relevant_list").each(function () {
            var uId = $(this).attr("id");

            if(id!=0){
                count=id
                if(id==1){
                    articleType=1;
                }else if (id==2)  {
                    articleType=2;
                }else{
                    articleType=3;
                }
            }else {
                count=id
                articleType="";
            }
            $(this).css({
                "display":"none"
            });
        });
        pageNumber=1;
        title = null;
        list()
    })


    //监听输入框事件
    $("#search").click(function(){
        title = $("#search_text").val();
        $(".muen_list").html("");
        list()
    });

    $(".btn_loading").click(function(){
        var id = parseInt($(this).attr("id"))
        if(id>pageSize-1){
            layer.msg("已加载完毕！！",{icon: 1});
            return
        }else{
            var c = ++id
            pageNumber+=1;
            if (pageTotal==pageNumber||pageTotal<pageNumber) {
                layer.msg("已加载完毕！！！",{icon: 1});
                return false;
            }
            $(".btn_loading").attr("id",c)
            list()
        }
    })

    list()
    function list(){
        $.ajax({
            type:"get",
            url:"/api/article",
            data:{
                pageSize: pageSize,
                articleType:articleType,
                pageNumber:pageNumber-1,
                title:title
            },
            async:true,
            dataType:"json",
            success:function(data){
                $(".wenzhang").html(data.totalCount);
                $(".commentTotal").html(data.totalCount);
                if (data.totalCount%pageSize==0){
                    pageTotal = data.totalCount/pageSize;
                } {
                    pageTotal = (data.totalCount/pageSize)+1;
                }
                var data = data.data;
                if(data!=undefined&&data!=null){
                    var str = "";
                    for(var i=0;i<data.length;i++){
                        var con = "";
                        if(data[i].introduction==""){
                            con = "博主很懒，没有留下摘要"
                        }else{
                            con=data[i].synopsis
                        }
                        var type = "";
                        if (data[i].articleType=="1"){
                            type="技术分享"
                        }else if (data[i].articleType=="2"){
                            type="生活点滴"

                        }  else {
                            type="更新公告"

                        }
                        str += "<div class='relevant_list row clear'  id='"+type+"'>" +
                            "<div class='list_img col-sm-4 col-md-4'>" +
                            "<img src='"+data[i].cover+"'/>" +
                            "</div>" +
                            "<div class='list_context col-xs-12 col-sm-8 col-md-8'>" +
                            "<div class='list_title'>" +
                            "<a href='/context/"+data[i].id+"' class='a_title'>" +
                            "<em style='color: orangered;font-size: 16px;'>『"+data[i].isOriginal+"』</em>"+data[i].title+"</a>" +
                            "</div>" +
                            "<div class='list_txt'>"+con+"</div>" +
                            "<div class='list_other clear'>" +
                            "<div class='other_time'>" +
                            "<img src='/img/shijian_riji.png'/>" +
                            "<span>"+data[i].createTime+"</span>" +
                            "</div>" +
                            "<div class='other_visit'>" +
                            "<img src='/img/fangwen_riji.png'/>" +
                            "<span>"+data[i].clickRate+"</span>" +
                            "</div>" +
                            "<div class='other_type'>" +
                            "<img src='/img/fenlei_riji.png'/>" +
                            "<span>"+type+"</span>" +
                            "</div>" +
                            "</div>" +
                            "</div>" +
                            "</div>"
                    }

                    $(".muen_list").append(str);
                }
            },
            error:function(err){
               // layer.msg("服务繁忙，请稍后再试！",{icon: 1});
            }
        });
    }


})


//----------------
/*
$(function() {
    $(".screen_ul>li").click(function() {
        $(this).addClass('selected').siblings().removeClass('selected').end().find(':radio').attr('cheked', true)
    });
    $(".screen_ipt").keyup(function() {
        setTimeout(function() {
            $('.relevant_list').hide().filter(":contains('" + ($(this).val()) + "')").show()
        }, 1000)
    });
    $(".screen_ul>li").click(function() {
        $(this).addClass('selected').siblings().removeClass('selected').end().find(':radio').attr('cheked', true);
        var id = $(this).attr("id");
        $(".relevant_list").each(function() {
            var uId = $(this).attr("id");
            if(id != 0) {
                if(id == uId) {
                    $(this).css({
                        "display": "block"
                    })
                } else {
                    $(this).css({
                        "display": "none"
                    })
                }
            } else {
                $(".relevant_list").css({
                    "display": "block"
                })
            }
        })
    });
    var n = Math.ceil($(".wenzhang").text() / 10);
    $(".btn_loading").click(function() {
        var r = $(".record").text();
        var sr = ++r;
        if(sr > n - 1) {
            return
        } else {
            $(".record").text(sr);
            title()
        }
    });
    title();

    function title() {
        $.ajax({
            url: "/api/article",
            data: {
                pageSize: $(".record").text() * 10,
                pageNumber:0
            },
            type: "get",
            dataType: "json",
            async: true,
            beforeSend: function() {},
            success: function(result) {
                msg=result.data

                console.log(msg);
                var str = "";
                if(msg != null) {
                    for(var i = 0; i < msg.length; i++) {
                        var type = "";
                        if (msg[i].articleType=="1"){
                            type="技术分享"
                        }else if (msg[i].articleType=="2"){
                            type="生活点滴"
                        }  else {
                            type="更新公告"
                        }
                        str += "<div class='relevant_list row clear'  id='" +msg[i].articleType + "'><div class='list_img col-sm-4 col-md-4'><img src='" + msg[i].cover + "'/></div><div class='list_context col-xs-12 col-sm-8 col-md-8'><div class='list_title'><a href='/context?id=" + msg[i].id + "' rel='" + msg[i].synopsis + "' class='a_title'><em style='color: orangered;font-size: 16px;'>『" + msg[i].isOriginal + "』</em>" + msg[i].title + "</a></div><div class='list_txt'>" + msg[i].synopsis + "</div><div class='list_other clear'><div class='other_time'><img src='/static/img/shijian_riji.png'/><span>" + msg[i].createTime + "</span></div><div class='other_visit'><img src='/static/img/fangwen_riji.png'/><span>" + msg[i].clickRate + "</span></div><div class='other_type'><img src='/static/img/fenlei_riji.png'/><span>" + type + "</span></div></div></div></div>"
                    }
                } else {};
                $(".muen_list").append(str)
            },
            error: function(err) {
                console.log(err)
            },
            complete: function() {}
        })
    }
})
*/
